#include "cn_cmd_exeattr.h"
#include "cn_data_flash.h"
#include  "cn_usart_x.h"
static MsgResult_t SetDevMsgAttr(uint8_t* sdata, uint16_t slen, uint8_t* rlen, uint8_t* rpara,uint16_t segtl,uint16_t segno);
static MsgResult_t GetDevMsgAttr(uint8_t* sdata, uint16_t slen, uint8_t* rlen, uint8_t* rpara,uint16_t segtl,uint16_t segno);
static MsgResult_t OpenComAttr(uint8_t* sdata, uint16_t slen, uint8_t* rlen, uint8_t* rpara,uint16_t segtl,uint16_t segno);
static MsgResult_t CloseComAttr(uint8_t* sdata, uint16_t slen, uint8_t* rlen, uint8_t* rpara,uint16_t segtl,uint16_t segno);
static MsgResult_t SendComMsgAttr(uint8_t* sdata, uint16_t slen, uint8_t* rlen, uint8_t* rpara,uint16_t segtl,uint16_t segno);
static uint8_t GetAttrNum(void);
static uint8_t p_data[1024];
const struct attr_exe_data SysAttrTable[] =
{
	{0x00, SetDevMsgAttr             },
	{0x01, GetDevMsgAttr             },
	{0x02,OpenComAttr                },
	{0x03,CloseComAttr                },	
	{0x04,SendComMsgAttr          },	
};

MsgResult_t Msg_Execute(uint8_t *sdata, uint32_t len)
{
	uint8_t ack_len = 0;
	struct comdata *pdata = (struct comdata *)p_data;
	uint8_t i;
	uint8_t count = GetAttrNum();
	MsgResult_t result = NO_CMD;
	struct comdata *qdata = (struct comdata *)sdata;
    const struct attr_exe_data* p_attr_exe = SysAttrTable;
	memcpy(pdata,sdata,len);
	for(i=0; i<count; i++)
	{
		if(p_attr_exe->cmd == pdata->cmd) 
		{
			//differentiate write and read attribute
			if(p_attr_exe->exe_attr != NULL)
			{
				result = p_attr_exe->exe_attr(pdata->data, 
												(pdata->length - 13),
												&ack_len,qdata->data + 1,
												pdata->segtl,
												pdata->segno); //pdata.length - 15数据域长度

			}
			break;
		}
		p_attr_exe++;
	}

	if(i == count)//NO_CMD
	{
		return result;
	}

	qdata->length = ack_len + 23;//the number 15 is other inherent length,一个字节是回应
	qdata->segtl = 1;
	qdata->segno = 1;
	*qdata->data  = result;
	return result;
}

static uint8_t GetAttrNum(void)
{
	return (sizeof(SysAttrTable)/sizeof(struct attr_exe_data));
}

static MsgResult_t SetDevMsgAttr(uint8_t* sdata, uint16_t slen, uint8_t* rlen, uint8_t* rpara,uint16_t segtl,uint16_t segno)
{
	cn_network_para *pdata = (cn_network_para *)sdata;
//	printf("SetDevMsgAttr \n");
	set_ap_message(pdata->ap_ip_addr,pdata->ap_gw_addr,pdata->ap_msk_addr);
	set_net_message(pdata->network_type,pdata->target_ip,pdata->target_port,pdata->local_port);
	set_sta_wifi(pdata->sta_ssid,pdata->sta_pwd);
	set_ap_wifi(pdata->ap_ssid,pdata->ap_pwd);
	set_dev_name(pdata->dev_name);
	tls_sys_reset();
    return COMPLETE;
}

static MsgResult_t GetDevMsgAttr(uint8_t* sdata, uint16_t slen, uint8_t* rlen, uint8_t* rpara,uint16_t segtl,uint16_t segno)
{
	u8 tmp[650] = {0};
	cn_network_para *pdata = (cn_network_para *)tmp;
	*rlen = 500;
//	printf("GetDevMsgAttr %d\n",*rlen);
	get_ap_message(&pdata->ap_ip_addr,&pdata->ap_gw_addr,&pdata->ap_msk_addr);
	get_net_message(&pdata->network_type,&pdata->target_ip,&pdata->target_port,&pdata->local_port);
	get_sta_wifi(pdata->sta_ssid,pdata->sta_pwd);
	get_ap_wifi(pdata->ap_ssid,pdata->ap_pwd);
	get_dev_name(pdata->dev_name);
#if 0
	printf("sta ssid:%s  pwd:%s \n",pdata->sta_ssid,pdata->sta_pwd);
	printf("ap ssid:%s  pwd:%s \n",pdata->ap_ssid,pdata->ap_pwd);
	printf("dev_name%s \n",pdata->dev_name);
#endif
	memcpy(rpara,pdata,*rlen);
    return COMPLETE;
}

static MsgResult_t OpenComAttr(uint8_t* sdata, uint16_t slen, uint8_t* rlen, uint8_t* rpara,uint16_t segtl,uint16_t segno)
{
#if 1
	cn_uart_options_t *opt = (cn_uart_options_t *)&sdata[1];
	switch (sdata[0])
	{
	case 1:
		set_usart1_config(opt);
		break;

	case 2:
		set_usart2_config(opt);
		break;

	case 3:
	set_usart3_config(opt);
	break;

	case 4:
	set_usart4_config(opt);
	break;	

	default:
		break;
	}
#endif
    return COMPLETE;
}
static MsgResult_t CloseComAttr(uint8_t* sdata, uint16_t slen, uint8_t* rlen, uint8_t* rpara,uint16_t segtl,uint16_t segno)
{
	switch (sdata[0])
	{
	case 1:
		set_usart1_colse();
		break;

	case 2:
		set_usart2_colse();
		break;

	case 3:
		set_usart3_colse();
		break;

	case 4:
		set_usart4_colse();
		break;	

	default:
		break;
	}
    return COMPLETE;
}
static MsgResult_t SendComMsgAttr(uint8_t* sdata, uint16_t slen, uint8_t* rlen, uint8_t* rpara,uint16_t segtl,uint16_t segno)
{
    return COMPLETE;
}


